// pages/order/order.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // user:wx.getStorageSync('user'),
    imageUrl:"http://cdn.pixabay.com/photo/2014/09/14/14/21/currywurst-445022_1280.jpg",
    imagelist: [
      "http://cdn.pixabay.com/photo/2014/09/14/14/21/currywurst-445022_1280.jpg",
      "http://cdn.pixabay.com/photo/2016/03/27/21/59/bread-1284438_1280.jpg"
    ],
    navList: [
      { value:1, title: '未完成', index: 0 },
      { value:2, title: '全部', index: 1 },
      { value:3, title: '已完成', index: 2 },
    ],
    chosed: 0,
    num: 3,
    token:wx.getStorageSync('token'),
    orderList:[
      {"code":"001",
      "food":"臭豆腐*1，腐乳*1",
      "price":"100",
      "orderid":"111",
      "time":"100s",
      "imageurl":"http://cdn.pixabay.com/photo/2016/03/27/21/59/bread-1284438_1280.jpg"
     },
     {"code":"002",
     "food":"臭豆腐*1，腐乳*1，老干妈*1",
     "price":"100",
     "orderid":"111",
     "time":"100s",
     "imageurl":"http://cdn.pixabay.com/photo/2016/03/27/21/59/bread-1284438_1280.jpg"
    },
    {"code":"003",
    "food":"，腐乳*1，老干妈*1，黄瓜丝儿*1 ,臭豆腐*1",
      "price":"100",
      "orderid":"111",
      "time":"100s",
      "imageurl":"https://cdn.pixabay.com/photo/2014/09/14/14/21/currywurst-445022_1280.jpg"
     },
     {"code":"004",
      "food":"腐乳*1，",
      "price":"100",
      "orderid":"111",
      "time":"100s",
      "imageurl":"https://cdn.pixabay.com/photo/2014/09/14/14/21/currywurst-445022_1280.jpg"
     },]
  },
  getunfinishedOrders: function() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/Orders/getUnfinishedOrdersById',
      method: 'POST',
      header:{
        //   wx.getStorageSync('token') 从缓存中取出token值
        "Authorization":wx.getStorageSync('token')
      },
      success: function(res) {
        var dataArray = res.data.orderList;
        // console.log(dataArray);
        // 在正确的页面对象上调用setData
        that.setData({
          orderList: dataArray
        });
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  getfinishedOrders: function() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/Orders/getFinishedOrdersById',
      method: 'POST',
      header:{
        //   wx.getStorageSync('token') 从缓存中取出token值
        "Authorization":wx.getStorageSync('token')
      },
      success: function(res) {
        var dataArray = res.data.orderList;
        // console.log(dataArray);
        // 在正确的页面对象上调用setData
        that.setData({
          orderList: dataArray
        });
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  getallOrders: function() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/Orders/getOrdersById',
      method: 'POST',
      header:{
        //   wx.getStorageSync('token') 从缓存中取出token值
        "Authorization":wx.getStorageSync('token')
      },
      success: function(res) {
        var dataArray = res.data.orderList;
        // console.log(dataArray);
        // 在正确的页面对象上调用setData
        that.setData({
          orderList: dataArray
        });
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },

  navChange(e) {
    //todo:切换样式  更改index,以便切换主体内容
    //通过更给chosed来更改样式和数据源
    let index = e.currentTarget.dataset.index;
    // console.log(index)
    this.setData({
      chosed: index,
    })
    switch(index){
      case 0:      
        console.log("未完成");
        //发送请求
        //未完成订单
        this.getunfinishedOrders();
        break;
      case 1:    
        console.log("全部");
        //发送请求
        //未完成订单
        this.getallOrders();
        break;
      case 2:  
        console.log("已完成");
        //发送请求
        //未完成订单
        this.getfinishedOrders();
        break;
      default:break;
    }
  },
  
  onTabjump:function(event){
    // console.log(event);
    let a=event.currentTarget.dataset.orderid;
    wx.setStorageSync('orderid', a);
    console.log(a)
    wx.navigateTo({
      url: '/pages/orderdetails/orderdetails',
      success:function(){
      console.log('jump success');
      },
      fail:function(){
        console.log('fail')
      },

    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  loginornot:function(){
    let user1=wx.getStorageSync('user');
    console.log(user1);
    if(user1==null){
      wx.redirectTo({
        url: '/pages/order_fail/order_fail',
        success:function(){
        console.log('jump success');
        },
        fail:function(){
        },
      })
    }
  },


  onLoad(options) {
    this.loginornot();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})